package com.sinosoft.dsp.component.bpm.facade.engine;

import java.util.ArrayList;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.NoneScoped;

import org.jbpm.api.ProcessInstance;
import org.jbpm.api.model.Event;

import com.sinosoft.dsp.component.bpm.util.JbpmConfig;
import com.sinosoft.dsp.platform.utils.SinoManagedBean;

/**
 * 流程实例管理服务门面
 * 
 * @author lichanghao
 * 
 */
@ManagedBean(name = "processInstanceFacade")
@NoneScoped
public class ProcessInstanceFacade {

	/**
	 * 创建流程实例
	 * 
	 * @param key
	 *            流程定义的key
	 * @return 流程实例
	 */
	public ProcessInstance createProcessInstance(String key) {
		JbpmConfig jbpmConfig = SinoManagedBean.findBean("jbpmConfig");
		ProcessInstance processInstance = jbpmConfig.getExecutionService()
				.startProcessInstanceByKey(key);
		return processInstance;
	}

	public ProcessInstance createProcessInstanceWithUserId(String key,
			String userId) {
		JbpmConfig jbpmConfig = SinoManagedBean.findBean("jbpmConfig");
		ProcessInstance processInstance = jbpmConfig.getExecutionService()
				.startProcessInstanceByKey(key,"");
		return processInstance;
	}

	/**
	 * 创建流程实例
	 * 
	 * @param key
	 *            流程定义id，processInstanceKey 流程实例的key
	 */
	public ProcessInstance createProcessInstance(String key,
			String processInstanceKey) {
		JbpmConfig jbpmConfig = SinoManagedBean.findBean("jbpmConfig");
		ProcessInstance processInstance = jbpmConfig.getExecutionService()
				.startProcessInstanceById(key, processInstanceKey);
		return processInstance;
	}

	/**
	 * 删除流程实例
	 * 
	 * @param processInstanceId
	 *            流程实例ID
	 */
	public boolean deleteProcessInstance(String processInstanceId) {
		// TODO Auto-generated method stub
		JbpmConfig jbpmConfig = SinoManagedBean.findBean("jbpmConfig");
		jbpmConfig.getExecutionService().deleteProcessInstance(
				processInstanceId);
		return false;
	}

	/**
	 * 强制结束流程实例
	 * 
	 * @param processInstanceId
	 *            流程实例ID
	 */
	public boolean forceFinishProcessInstance(String processInstanceId) {
		// TODO Auto-generated method stub
		JbpmConfig jbpmConfig = SinoManagedBean.findBean("jbpmConfig");
		jbpmConfig.getExecutionService().endProcessInstance(processInstanceId,
				Event.END);
		return false;
	}

	/**
	 * 获取流程实例
	 * 
	 * @param processInstanceId
	 *            流程实例ID
	 */
	public ProcessInstance getProcessInstance(String processInstanceId) {
		JbpmConfig jbpmConfig = SinoManagedBean.findBean("jbpmConfig");
		ProcessInstance processInstance = jbpmConfig.getExecutionService()
				.findProcessInstanceById(processInstanceId);
		return processInstance;
	}

	/**
	 * 获取指定流程定义的所有运行中的流程实例
	 * 
	 * @param key
	 *            流程定义ID
	 */
	public List<ProcessInstance> findProcessInstancesByDefinition(String key) {
		JbpmConfig jbpmConfig = SinoManagedBean.findBean("jbpmConfig");
		List<ProcessInstance> processInstanceList = jbpmConfig
				.getExecutionService().createProcessInstanceQuery()
				.processDefinitionId(key).notSuspended().list();
		return processInstanceList;
	}

	/**
	 * 获得所有运行中的流程实例
	 * 
	 */
	public List<ProcessInstance> findProcessInstances() {
		List<ProcessInstance> processInstanceList = new ArrayList<ProcessInstance>();
		JbpmConfig jbpmConfig = SinoManagedBean.findBean("jbpmConfig");
		processInstanceList = jbpmConfig.getExecutionService()
				.createProcessInstanceQuery().notSuspended().list();

		return processInstanceList;
	}

}
